#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct node
{
	int data;
	struct node *next;
}*list;
list create(list head){
    if(head==NULL){
        head=(list)malloc(sizeof(struct node));
        head->data=-1;
        head->next=NULL;
    }
    int n;
    scanf("%d",&n);
    while(n!=-1){//头插法
        list node=(list)malloc(sizeof(struct node));
        node->data=n;
        node->next=head->next;
        head->next=node;
        scanf("%d",&n);
    }
    return head;
}
list UnionSet(list Q,list P){
    list R=(list)malloc(sizeof(struct node));
    list p,q,r=R;
    q=Q->next;
    while(q){//Q中所有元素并入R，尾插法
        r->next=q;
        r=q;
        q=q->next;
    }
    p=P->next;
    while(p!=NULL){//从P中找出与Q不同的结点，并入R
        q=Q->next;
        int flag=0;
        while(q!=NULL){
            if(q->data==p->data){//P中存在和已经并入R的Q相等的元素，继续比较P的下一个元素
                flag=1;
                break;
            }
            q=q->next;
        }
        if(flag==0){//Q中不存在和此时节点p存储数据相等的元素
            r->next=p;
            r=p;
        }
        p=p->next;
    }
    r->next=NULL;
    return R;
}
void printList(list head){
    list p=head;
    while(p){
        printf("%d->",p->data);
        p=p->next;
    }
    printf("\n");
}
int main(){
    list headA=NULL,headB=NULL;
    headA=create(headA);
    printList(headA);
    getchar();
    headB=create(headB);
    printList(headB);
    list head=UnionSet(headA,headB);
    printList(head);
    return 0;
}